package com.hd123.rumba.organization;

import junit.framework.TestCase;
import net.sf.hibernate.Transaction;

import com.hd123.rumba.runtime.Configuration;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.user.IUserMgr;

public class TCOrganizationMgr extends TestCase {

  private IOrganizationMgr orgMgr;
  private IUser sysUser = null;
  private Transaction tx;
  private String uuid;

  public static void main(String[] args) {
    junit.textui.TestRunner.run(TCOrganizationMgr.class);
  }

  public TCOrganizationMgr(String name) {
    super(name);
    try {
      Configuration.getInstance().config("/rumba.xml;/myrumba.xml");
      orgMgr = (IOrganizationMgr) NamingMgr.getInstance().lookupMgr(IOrganizationMgr.class);
      IUserMgr userMgr = (IUserMgr) NamingMgr.getInstance().lookupMgr(IUserMgr.class);
      sysUser = userMgr.getSysUser();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  protected void setUp() throws Exception {
    super.setUp();
  }

  protected void tearDown() throws Exception {
    super.tearDown();
  }

  public void test_1() throws Exception {
    try {
      IOrganization org = orgMgr.createOrganization(null, "O01");

      org.setName("测试组织01");
      org.setRemark("这是说明文本。");

      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.saveOrganization(org, COrganization.VALUE_STATE_USING, sysUser, org.getOca());
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

      org = orgMgr.getOrganizationByCode("O01");
      uuid = org.getUuid();

      org.setCode("O02");
      org.setName("测试组织02");
      org.setRemark("这是修改后的说明文本。");

      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.saveOrganization(org, COrganization.VALUE_STATE_USING, sysUser, org.getOca());
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

      org = orgMgr.getOrganizationByCode("O02");

      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.saveOrganization(org, COrganization.VALUE_STATE_DELETED, sysUser, org.getOca());
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

      IOrganization org2 = orgMgr.getOrganizationByCode("O02");
      assertNull(org2);

      org = orgMgr.getOrganization(uuid);
      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.removeOrganization(org);
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void test_2() throws Exception {
    try {
      IOrganization org = orgMgr.createOrganization(null, "O10");
      org.setName("测试组织-父组织");

      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.saveOrganization(org, COrganization.VALUE_STATE_USING, sysUser, org.getOca());
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

      IOrganization org2 = orgMgr.createOrganization(org, "O101");
      org2.setName("测试组织-子组织101");
      IOrganization org3 = orgMgr.createOrganization(org, "O102");
      org3.setName("测试组织-子组织102");

      tx = HibernateSession.getSession().beginTransaction();
      try {
        orgMgr.saveOrganization(org2, COrganization.VALUE_STATE_USING, sysUser, org.getOca());
        orgMgr.saveOrganization(org3, COrganization.VALUE_STATE_USING, sysUser, org.getOca());
        tx.commit();
      } catch (Exception e) {
        tx.rollback();
        throw e;
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}
